package com.example.martian.utils;

import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Component
public class BASE64 {
    /**
     * 将本地图片进行Base64位编码
     * <p>
     * imgUrl 图片的url路径，如e:\\123.png
     *
     * @return
     */
    public static String encodeImgageToBase64(File imageFile) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        // 其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imageFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        return encoder.encode(data);
    }


    public Map<String,Object> uploadingCard(){
        Map<String,Object> mapObj = null;
        //获取本地的绝对路径图片
        File file = new File("E:\\photo\\beimian.jpg");
        //进行BASE64位编码
        String imageBase = BASE64.encodeImgageToBase64(file);
        imageBase = imageBase.replaceAll("\r\n", "");
        imageBase = imageBase.replaceAll("\\+", "%2B");
        //百度云的文字识别接口,后面参数为获取到的token
        String httpUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token="+BaiDuOCR.getAuth();
        //id_card_side=front 识别正面    id_card_side=back  识别背面
        String httpArg = "detect_direction=true&id_card_side=front&image=" + imageBase;
        String jsonResult = BaiDuOCR.request(httpUrl, httpArg);
        //System.out.println("返回的结果--------->" + jsonResult);
        HashMap<String, String> map = BaiDuOCR.getHashMapByJson(jsonResult);
        for (String key : map.keySet()) {
            mapObj = new HashMap<>();
            mapObj.put(key,map.get(key));
        }
        return mapObj;
    }

    /**
     * 图片识别
     * @param picOcr
     * @throws Exception
     */

    public Map<String, Object> picOCR(MultipartFile picOcr) throws Exception {
        Map<String,Object> mapObj = new HashMap<>();
        byte[] data = null;
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String base64 = base64Encoder.encode(picOcr.getBytes());
        base64 = base64.replaceAll("\r\n", "");
        base64 = base64.replaceAll("\\+", "%2B");
        String httpUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + BaiDuOCR.getAuth();
        //正面照
        String httpFront = "detect_direction=true&id_card_side=front&image=" + base64;
        //背面照
        String httpBack = "detect_direction=true&id_card_side=back&image=" + base64;
        String jsonResult = BaiDuOCR.request(httpUrl, httpFront);
        HashMap<String, String> map = BaiDuOCR.getHashMapByJson(jsonResult);
        for (String key : map.keySet()) {
            mapObj.put(key,map.get(key));
        }
        return mapObj;
    }


}